using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Karasoft.Extension;

namespace Karasoft.Web.UI
{ 
    
    #region properites
    [DefaultProperty("Date")]
    [ToolboxData("<{0}:HijriLabel runat=server></{0}:HijriLabel>")]
    public class HijriLabel : Label
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime? Date
        {
            get
            {
                if (ViewState["Date"] != null)
                    return (DateTime?)ViewState["Date"];
                else
                    return null;
            }

            set
            {
                ViewState["Date"] = value;
            }
        }
        [DefaultValue(true)]
        public bool UseUmAlQura
        {
            get
            {
                if (ViewState["UseUmAlQura"] != null)
                    return (bool)ViewState["UseUmAlQura"];
                else
                    return true;
            }
            set
            {
                ViewState["UseUmAlQura"] = value;
            }
        }
        public string DateFormat
        {
            get
            {
                if (ViewState["DateFormat"] != null)
                    return ViewState["DateFormat"].ToString();
                else
                    return Culture.DateTimeFormat.LongDatePattern;
            }
            set
            {
                ViewState["DateFormat"] = value;
            }
        }
        
        public System.Globalization.CultureInfo Culture
        {
            get
            {
                System.Globalization.CultureInfo _Culture = new System.Globalization.CultureInfo("ar-SA", false);
                if(UseUmAlQura)
                    _Culture.DateTimeFormat.Calendar = new System.Globalization.UmAlQuraCalendar();
                else
                    _Culture.DateTimeFormat.Calendar = new System.Globalization.HijriCalendar();
                return _Culture;
            }
            //set { _Culture = value; }
        }

        public string DateText
        {
            get
            {
                if (Date.HasValue)
                {
                    return Date.ToHijriDate(DateFormat);
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        #endregion

        protected override void RenderContents(HtmlTextWriter output)
        {
            if (Date.HasValue)
            {
                Text = Date.ToHijriDate(DateFormat);
            }
            else
            {
                Text = string.Empty;
            }
            output.Write(Text);
        }
    }
}
